package coreservlets;

import java.util.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import model.Customer;

@ManagedBean(name="enter_order")
public class EnterInfoPageBean
{
   private String username;
   private String password;
   
   public String getUsername() 
   {
      return(username);
   }
   public void setUsername(String username)
   {
      this.username = username.trim();
   }

   public String getPassword() 
   {
      return(password);
   }
   public void setPassword(String password)
   {
      this.password = password.trim();
   }

   
   String dbName = "testdatabase";
   String dbuserName = "cromvary";
   String dbpassword = "9Kuskup1";
   String hostname = "testdbinstance.crkptu8dcf8x.us-west-2.rds.amazonaws.com";
   String port = "3306";

   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   String DB_URL = "jdbc:mysql://" + hostname + ":" + port + "/" + dbName + "?user=" + dbuserName + "&password=" + dbpassword;

   public String saveInDB() throws SQLException, ClassNotFoundException
   {
      String un = "";
      String pw = "";
      
      un = this.username;
      pw = this.password;

      Connection conn = null;
      Statement stmt = null;

      try
      {
    	 String sql = "";

         Class.forName("com.mysql.jdbc.Driver");
         conn = DriverManager.getConnection(DB_URL);
         stmt = conn.createStatement();

         sql = "INSERT INTO " + dbName + ".testtable (NAME,PASSWORD) VALUE ('" + un + "','" + pw + "');";

         stmt.executeUpdate(sql);

         stmt.close();
         conn.close();

      }
      finally{
          //finally block used to close resources
          try{
             if(stmt!=null)
                stmt.close();
          }catch(SQLException se2){
          }// nothing we can do

          try{
             if(conn!=null)
                conn.close();
          }catch(SQLException se){
             se.printStackTrace();
          }//end finally try
      }
	return null;
   }

	
   public String returnToMainPage()
   {	   
	   return("mainpage");
   }

}